package com.hzsai.controller;

import com.hzsai.service.FileService;
import com.hzsai.utils.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

/**
 * hzsai 2019/7/27
 * usage:
 **/
@Api(tags = "文件上传、下载")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService service;

    @ApiOperation(value = "通过HttpServletRequest提交文件")
    @PostMapping("/upload/request/")
    public ResponseMsg uploadFile(HttpServletRequest request) {
        if (request == null)
            return ResponseMsg.fail(110, "上传失败", null);
        MultipartHttpServletRequest rq = null;
        if (request instanceof MultipartHttpServletRequest)
            rq = (MultipartHttpServletRequest) request;
        else
            return ResponseMsg.fail(110, "上传失败", null);
        MultipartFile file = rq.getFile("file");
        String fileName = file.getOriginalFilename();
        File f = new File(fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseMsg.fail(110, "上传失败", null);
        }
        return ResponseMsg.success("上传成功");
    }

    @ApiOperation(value = "通过Multipart上传文件")
    @PostMapping("/upload/multipart/")
    public ResponseMsg uploadMultiPart(MultipartFile multipartFile) {
        if (multipartFile == null) {
            System.out.println("multipartFile为空");
            return ResponseMsg.fail(110, "上传失败", null);
        }
        String fileName = multipartFile.getOriginalFilename();
        File file = new File(fileName);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseMsg.fail(110, "上传失败", null);
        }
        return ResponseMsg.success("上传成功");
    }

    @ApiOperation(value = "通过Part直接上传文件")
    @PostMapping("upload/part/")
    public ResponseMsg uploadPart(Part part) {
        if (part == null) {
            System.out.println("Part为空");
            return ResponseMsg.fail(110, "上传失败", null);
        }
        String filename = part.getSubmittedFileName();
        File file = new File(filename);

        try {
            part.write(file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseMsg.fail(110, "上传失败", null);
        }

        return ResponseMsg.success("上传成功");
    }

    @ApiOperation(
        value = "通过Id来下载用户信息SQL文件",
        notes = "在windows系统下，将文件保存在C:\\Users\\hzsai\\AppData\\Local\\Temp\\里\n\n" +
                "在Linux系统下，将文件保存在/tmp/里"
    )
    @GetMapping("/downloadInfoById/{id}")
    public void downloadUserSQLFile(@PathVariable("id") Integer id,
                                           HttpServletRequest req, HttpServletResponse rep) {
        service.downloadUserSQLFile(id, req, rep);
    }

    @ApiOperation(value = "备份导出数据库sql文件", notes = "导出的是springboot库")
    @GetMapping("/exportDB")
    public void exportADIIDB(HttpServletRequest request, HttpServletResponse response) {
        service.exportADB(request, response);
    }
}
